<template>
  <div class="app-container">
    <el-button type="primary" @click="handlerAdd">添加</el-button>
    <el-table :data="dataList" style="width: 100%;margin-top:30px;" v-loading.body="listLoading" border>
      <el-table-column align="center" label="序号" width="100">
        <template scope="scope">
          {{scope.row.index}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类名称" width="200">
        <template scope="scope">
          {{scope.row.categoryName}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑分类':'添加分类'">
      <el-form :rules="rules" ref="category" :model="category" label-width="80px" label-position="left">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="category.categoryName" placeholder="分类名称"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmAdd">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { page, save, del } from 'api/category'
  export default {
    data() {
      return {
        listLoading: false,
        dialogVisible: false,
        dialogType: 'new',
        category: { categoryName: undefined },
        dataList: [],

        total: 1,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        rules: {
          categoryName: [{
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.getlist()
    },

    methods: {
      getlist() {
        this.listLoading = true
        page(this.listQuery).then(response => {
          var data = response.data
          this.dataList = data.rows;
          this.total = data.total;
          this.listLoading = false;
          for (var ix in this.dataList) {
            this.dataList[ix].index = parseInt(ix, 10) + 1;
          }
          this.listLoading = false
        })
      },
      handlerAdd() {
        this.dialogVisible = true
      },
      handleEdit(scope) {
        this.dialogType = 'edit'
        this.dialogVisible = true
        this.category.id = scope.row.id
        this.category.status = 1
        this.category.categoryName = scope.row.categoryName
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
      },
      handleDel(scope) {
        this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          scope.row.status = 0
          del(scope.row).then(res => {
            if (res.status == 200) {
              this.$success("删除成功")
              this.getlist()
            } else {
              this.$error(res.message)
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      confirmAdd() {
        this.$refs.category.validate(valid => {
          if (valid) {
            save(this.category).then(response => {
              if (response.status == 200) {
                this.$success('保存成功')
                this.getlist()
              } else {
                this.$error(response.message)
              }
              const isEdit = this.dialogType === 'edit'
              this.dialogVisible = false
            })
          }

        })

      }
    },
  }
</script>